import { CONFIG } from 'api/config'
import { getWeChatOauth } from 'api/wechat'

export function getUrlParams() {
  let tokenParam = getUrlParam()
  console.log(tokenParam)
  // 首次进入项目，获取appid并存储
  const reg = /(\/).\d{0,}(\/)/
  let getNum = /\d{1,}/
  let appidArray = window.location.href.match(reg)
  // console.log('appidArray:-----' + appidArray)
  if (!appidArray) {
    console.log('!appidArray')
    CONFIG.APPID = 10
    localStorage.uuAppId = 10
    let url = window.location.href.split('?')[0]
    window.location.href = url
    return
  }
  let appid = appidArray[0].match(getNum)
  // console.log('appid:-----' + appid)
  if (!appid) {
    console.log('!appid')
    CONFIG.APPID = 10
    localStorage.uuAppId = 10
    let url = window.location.href.split('?')[0]
    alert(url)
    window.location.href = url
    return
  }
  // 如果是页面刷新或者刚进入时，更新appid
  if (appid[0]) {
    CONFIG.APPID = appid[0]
    // 如果当前appid与本地不一样时，更新token
    if (localStorage.uuAppId && localStorage.uuAppId !== appid[0]) {
      localStorage.uuAppId = appid[0]
      if (localStorage.uuToken) {
        getWeChatOauth()
        return
      }
    } else {
      localStorage.uuAppId = appid[0]
    }
  }
  // 授权后若url里accessToken有值
  if (tokenParam.hasOwnProperty('accessToken') && tokenParam.accessToken) {
    console.log('授权后若url里accessToken有值')
    if (window.location.href.indexOf('wxpay') !== -1) {
      if (localStorage.uuAppId !== appid || !localStorage.uuAppId || !localStorage.uuOpenId) {
        localStorage.uuOpenId = tokenParam.openId
        localStorage.uuAppId = appid
      }
    } else {
      CONFIG.HEADER.accessToken = tokenParam.accessToken
      localStorage.uuToken = tokenParam.accessToken
      localStorage.uuOpenId = tokenParam.openId
      localStorage.expiresIn = JSON.stringify({
        validity: Date.parse(new Date()) / 1000,
        validityDay: tokenParam.expiresIn
      })
    }
    let relationInfo = {
      relationId: tokenParam.openId,
      relationPlatform: 'WeChat'
    }
    localStorage.uuRelationInfo = JSON.stringify(relationInfo)
  } else {
    // 授权后若url里accessToken无值，只有openid
    console.log('授权后若url里accessToken无值，只有openid')
    let url = window.location.href.split('?')[0]
    if (tokenParam.openId) {
      let relationInfo = {
        relationId: tokenParam.openId,
        relationPlatform: 'WeChat'
      }
      localStorage.uuOpenId = tokenParam.openId
      localStorage.uuRelationInfo = JSON.stringify(relationInfo)
      // 去登录页面
      let routerUrl = url.slice(-4)
      // console.log('routerUrl: ----- ' + routerUrl)
      if (routerUrl === 'home') {
        console.log('home:' + url)
      } else if (window.location.href.indexOf('wxpay') !== -1) {
        if (localStorage.uuAppId !== appid || !localStorage.uuAppId || !localStorage.uuOpenId) {
          localStorage.uuOpenId = tokenParam.openId
          localStorage.uuAppId = appid
        }
      } else {
        // 当未登录时扫ic卡需要做登录后跳转的数据保留
        if (window.location.href.indexOf('bindCard') !== -1 || window.location.href.indexOf('bindcard') !== -1) {
          let icCardCode = window.location.href.split('/bindCard/')[1]
          localStorage.bindCardType = JSON.stringify({
            type: 'NEED_LOGIN',
            icCardCode: icCardCode
          })
        }
        // 截取search前的url
        // let url = window.location.href.split('?')[0]
        let pathname = window.location.pathname
        // 以pathnam作为截取点
        let url2 = url.split(pathname)[0]
        console.log(url2)
        window.location.href = `${url2}/${CONFIG.APPID}/login`
      }
    } else if (tokenParam.relationId) {
      console.log('tokenParam.relationId')
      localStorage.uuAppId = tokenParam.bizAppId
      localStorage.payAppId = tokenParam.bizAppId
      localStorage.uuOpenId = tokenParam.relationId
    }
  }
}

function getUrlParam() {
  // console.log('getUrlParam')
  let args = null
  if (args === null) {
    if (window.location.search === '') return ''
    let queryArray = window.location.search.substring(1).split('&')
    let i
    args = {}
    for (i = 0; i < queryArray.length; i++) {
      let match = queryArray[i].match(/([^=]+)=([^=]+)/)
      if (match !== null) {
        args[match[1]] = match[2]
      }
    }
  }
  window.location.search = ''
  return args === undefined ? '' : args
}